package cn.edu.sdjzu.knoverse.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.edu.sdjzu.knoverse.model.dto.system.dept.DeptAddDto;
import cn.edu.sdjzu.knoverse.model.dto.system.dept.DeptDto;
import cn.edu.sdjzu.knoverse.model.dto.system.dept.DeptUpdateDto;
import cn.edu.sdjzu.knoverse.model.entity.system.Dept;
import cn.edu.sdjzu.knoverse.model.vo.result.PageResult;
import cn.edu.sdjzu.knoverse.model.vo.result.Result;
import cn.edu.sdjzu.knoverse.model.vo.result.ResultCodeEnum;
import cn.edu.sdjzu.knoverse.model.vo.system.DeptVo;
import cn.edu.sdjzu.knoverse.service.DeptService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "系统部门", description = "部门相关接口")
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @SaCheckPermission("dept::getDeptList")
    @Operation(summary = "分页查询部门", description = "分页查询部门")
    @GetMapping("getDeptList/{page}/{limit}")
    public Result<PageResult<DeptVo>> getDeptList(
            @Parameter(name = "page", description = "当前页", required = true)
            @PathVariable("page") Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            DeptDto dto) {
        Page<Dept> pageParams = new Page<>(page, limit);
        PageResult<DeptVo> pageResult = deptService.getDeptList(pageParams, dto);
        return Result.success(pageResult);
    }

    @SaCheckLogin
    @Operation(summary = "获取所有部门", description = "获取所有部门")
    @GetMapping("noManage/getAllDeptList")
    public Result<List<DeptVo>> getAllDeptList() {
        List<DeptVo> voList = deptService.getAllDeptList();
        return Result.success(voList);
    }

    @SaCheckPermission("dept::addDept")
    @Operation(summary = "添加部门", description = "添加部门")
    @PostMapping("addDept")
    public Result<String> addDept(@Valid @RequestBody DeptAddDto dto) {
        deptService.addDept(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @SaCheckPermission("dept::updateDept")
    @Operation(summary = "更新部门", description = "更新部门")
    @PutMapping("updateDept")
    public Result<String> updateDept(@Valid @RequestBody DeptUpdateDto dto) {
        deptService.updateDept(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @SaCheckPermission("dept::deleteDept")
    @Operation(summary = "删除部门", description = "删除部门")
    @DeleteMapping("deleteDept")
    public Result<String> deleteDept(@RequestBody List<Long> ids) {
        deptService.deleteDept(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}
